package com.controller;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.Chapter;
import com.beans.Novel;
import com.service.impl.ChapterServiceImpl;
import com.service.impl.NovelServiceImpl;

@Controller
@RequestMapping("/content")
public class ContentController {
	@Resource
	ChapterServiceImpl service;
	@Resource
	NovelServiceImpl novelservice;


	 @GetMapping("/getchapter")
	 public String updateChapter(String  novelname,ModelMap m) {
		 Chapter chapter=service.getChapterId(1,novelname);
		Novel novel = novelservice.getNovelByName(novelname);
		m.put("novel", novel);
		 m.put("chapter", chapter);
	     return "content/content";
	     }

	@GetMapping("/nextchapter")
	public String nextChapter(int chapterid, String novelname,int chapternum, ModelMap m) {
		if(chapterid>chapternum) {
			chapterid-=1;
		}
		Chapter chapter = service.getChapterId(chapterid, novelname);
		Novel novel = novelservice.getNovelByName(novelname);
		m.put("chapter", chapter);
		m.put("novel", novel);
		return "content/content";
	}


	@GetMapping("/prechapter")
	public String preChapter(int chapterid, String novelname, ModelMap m) {
		if(chapterid<1) {
			chapterid+=1;
		}
		Chapter chapter = service.getChapterId(chapterid, novelname);
		Novel novel = novelservice.getNovelByName(novelname);
		m.put("chapter", chapter);
		m.put("novel", novel);
		return "content/content";
	}

}
